from . import api_blueprint
from app.model.homework import Homework
from flask import request, json
from app import db


# get some homework
@api_blueprint.route('/homework/<int:page_num>', methods=['GET'])
def get_homework_by_page(page_num=1):
    cos_id = request.values.get('Cos_id')
    if cos_id is None:
        homework = Homework.query.paginate(page_num, 10, False).items
    else:
        homework = Homework.query.filter(Homework.Cos_id == cos_id).paginate(page_num, 10, False).items
    return json.dumps(homework, default=Homework.to_json)


# get homework by HW_id
@api_blueprint.route('/homework', methods=['GET'])
def get_homework_by_hw_id():
    hw_id = request.values.get('HW_id')
    if hw_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'HW_id is missing'
        }), 400
    homework = Homework.query.filter(Homework.HW_id == hw_id).first()
    return json.dumps(homework, default=Homework.to_json)


# delete homework by hw_id
@api_blueprint.route('/homework', methods=['DELETE'])
def delete_homework_by_hw_id():
    hw_id = request.values.get('HW_id')
    if hw_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'HW_id is missing'
        }), 400
    row = Homework.query.filter(Homework.HW_id == hw_id).first()
    if row is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target homework is not found'
        }), 400
    try:
        db.session.delete(row)
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'delete failed'
        }), 500
    # return cos_id
    return json.dumps({
        'status': 'success'
    })


# modify homework
@api_blueprint.route('/homework/<string:hw_id>', methods=['PUT'])
def modify_homework(hw_id):
    homework = Homework.query.filter(Homework.HW_id == hw_id).first()
    if request.values.get('Cos_id') is not None:
        homework.Cos_id = request.values.get('Cos_id')
    if request.values.get('HW_req') is not None:
        homework.HW_req = request.values.get('HW_req')
    if request.values.get('HW_ddl') is not None:
        homework.HW_ddl = request.values.get('HW_ddl')
    if request.values.get('HW_address') is not None:
        homework.HW_address = request.values.get('HW_address')
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'modify homework failed'
        }), 500
    # return cos_id
    return json.dumps({
        'status': 'success'
    })


# add homework
@api_blueprint.route('/homework', methods=['POST'])
def add_homework():
    hw_id = request.values.get('HW_id')
    if hw_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'HW_id is missing'
        }), 400

    record = Homework()

    if request.values.get('HW_id') is not None:
        record.HW_id = request.values.get('HW_id')
    if request.values.get('Cos_id') is not None:
        record.Cos_id = request.values.get('Cos_id')
    if request.values.get('Cos_id') is not None:
        record.Cos_id = request.values.get('Cos_id')
    if request.values.get('HW_req') is not None:
        record.HW_req = request.values.get('HW_req')
    if request.values.get('HW_ddl') is not None:
        record.HW_ddl = request.values.get('HW_ddl')
    if request.values.get('HW_address') is not None:
        record.HW_address = request.values.get('HW_address')

    db.session.add(record)

    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })
